package example.service;

import example.mapper.UserMapper;
import example.pojo.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("userServiceImpl")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public SysUser login(String account, String password) {
        SysUser user = userMapper.login(account, password);
        return user;

    }

    @Override
    public int add(SysUser user) {
        int count = 0;
        try {
            count = userMapper.add(user);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return count;
    }

    @Override
    public int upd(SysUser user) {
        int count = 0;
        try {
            count = userMapper.upd(user);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return count;
    }

    @Override
    public int del(Integer id) {
        int count = 0;
        try {
            count = userMapper.del(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return count;
    }

    @Override
    public SysUser getUserByIdOrAccount(Integer id, String account) {
        SysUser user = null;
        try {
            user = userMapper.getUserByIdOrAccount(id == null ? null : id, account);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return user;
    }

    @Override
    public List<SysUser> pageSelectUser(Integer pageIndex, Integer pageSize, String account, Integer roleId) {
        List<SysUser> userList = userMapper.pageSelectUser((pageIndex - 1) * pageSize, pageSize, account, roleId);
        return userList;
    }

    @Override
    public int count(String account, Integer roleId) {
        int count = userMapper.count(account, roleId);
        return count;
    }

    @Override
    public int accountExist(String account) {
        int count = 0;
        try {
            count = userMapper.accountExist(account);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return count;
    }

    @Override
    public List<SysUser> getUserBindRoleId(Integer rid) {
        List<SysUser> userList = new ArrayList<>();
        try {
            userList = userMapper.getUserBindRoleId(rid);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return userList;
    }

    @Override
    public int delUserByRoleId(Integer id) {
        int count = 0;
        try {
            count = userMapper.delUserByRoleId(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return count;
    }

    @Override
    public int checkOldPwd(Integer id, String oldPassword) {
        int count = 0;
        try {
            count = userMapper.checkOldPwd(id, oldPassword);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return count;
    }

    @Override
    public int updatePwd(Integer id, String oldPassword) {
        int count = 0;
        try {
            count = userMapper.updatePwd(id, oldPassword);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return count;
    }

    public UserMapper getUserMapper() {
        return userMapper;
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
}
